﻿#include <iostream>
#include<algorithm>
using namespace std;
struct stu
{
	string name;
	int y;
	int m;
	int d;
	int id;
};
const int N = 110;
struct stu s[N];
int cmp(struct stu s1, struct stu s2)
{
	if (s1.y != s2.y)
		return s1.y < s2.y;
	else if (s1.m != s2.m)
		return s1.m < s2.m;
	else if (s1.d != s2.d)
		return s1.d < s2.d;
	else
		return s1.id > s2.id;
}
int main()
{
	int n = 0;
	cin >> n;
	int i = 0;
	//输⼊

		for (i = 0; i < n; i++)
		{
			cin >> s[i].name >> s[i].y >> s[i].m >> s[i].d;
			s[i].id = i;
		}
	sort(s + 0, s + n, cmp);
	for (i = 0; i < n; i++)
	{
		cout << s[i].name << endl;
	}
	return 0;
}